package cn.itcast.controller;


import cn.itcast.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
//包括拦截器
@Controller
@RequestMapping("/login")
public class LoginController {
    /**
     * 登录页面
     * @return
     */
    @RequestMapping(value = "/to_login",method= RequestMethod.GET)
    public String login(){
        //返回pages里的login.jsp
        return "login";
    }

    /**
     * 用户登陆
     * @param user
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/do_login",method = RequestMethod.GET)
    public String doLogin(User user, Model model, HttpSession session){
        String name=user.getUsername();
        String password=user.getPassword();
        System.out.println(name+ password);
        if(name.equals("123") && password.equals("123")){
            session.setAttribute("user",user);
            //重定向到/login/main
            return "redirect:main";
        }else {
            model.addAttribute("msg", "用户名错误");
            //返回pages里的login.jsp
            return "login";
        }

    }


    /**
     * 进入主页
     * @return
     */
    @RequestMapping(value = "/main",method= RequestMethod.GET)
    public String main(){
        //返回pages里的main.jsp
        return "main";
    }

    /**
     * 登出
     * @param session
     * @return
     */

    @RequestMapping(value = "/logout",method= RequestMethod.GET)
    public String logout(HttpSession session){
        //清除session
        session.invalidate();
        //跳转到登陆页面
        //重定向到/login/to_login
        return "redirect:to_login";
    }


}
